/*
 * TimerLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.UIUtils;

/**
 * 倒计时的时钟
 */
public class TimerLayout extends LinearLayout {

    private TextView mTvHour;

    private TextView mTvMinute;

    private TextView mTvSecond;

    private TimerTask mTimerTask;

    private TextView mTvDivider1;

    private TextView mTvDivider2;

    /**
     * 剩余的时间，由后台给出，精确到秒
     */
    private long leaveTime;

    /**
     * 当前剩余的 小时
     */
    private long hour;

    /**
     * 当前剩余的 分钟
     */
    private long minute;

    /**
     * 当前剩余的 秒
     */
    private long second;

    /**
     * 不计时
     */
    private boolean unTimer;

    private boolean withoutBg;

    private OnTimeChangeListener mListener;

    private LinearLayout mRootView;

    private String timerString;

    public TimerLayout(Context context) {
        this(context, null);
    }

    public TimerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRootView = (LinearLayout) View.inflate(context, R.layout.timer_layout, this);

        mTvHour = (TextView) mRootView.findViewById(R.id.tv_hour);
        mTvMinute = (TextView) mRootView.findViewById(R.id.tv_minute);
        mTvSecond = (TextView) mRootView.findViewById(R.id.tv_second);
        mTvDivider1 = (TextView) mRootView.findViewById(R.id.tv_divider_1);
        mTvDivider2 = (TextView) mRootView.findViewById(R.id.tv_divider_2);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TimerLayout);

        withoutBg = typedArray.getBoolean(R.styleable.TimerLayout_without_bg, false);

        typedArray.recycle();

        if (withoutBg) {
            withoutBackgroundStyle();
        }
    }

    private void withoutBackgroundStyle() {
        mTvHour.setVisibility(GONE);
        mTvMinute.setVisibility(GONE);
        mTvSecond.setVisibility(GONE);
        mTvDivider1.setVisibility(GONE);
        mTvDivider2.setVisibility(GONE);
    }

    public String getTimeString() {
        return timerString;
    }

    public void setDividerColor(int color) {
        mTvDivider1.setTextColor(color);
        mTvDivider2.setTextColor(color);
    }

    /**
     * 实时计算，时分秒
     */
    private void calculateTime(long leaveTime) {

        if (leaveTime < 0) {
            return;
        }

        hour = leaveTime / 3600;
        minute = leaveTime % 3600 / 60;
        second = leaveTime % 3600 % 60;

        // 刷新时间的UI
        refreshTimeUI();

        if (leaveTime == 0) {
            if (mTimerTask != null) {
                mTimerTask.stopTimer();
            }
        }

        if (mListener != null) {
            mListener.onLeaveTimeChanged(getId(), leaveTime);
        }
    }

    private String getTimeText(long time) {
        return unTimer ? "99" : (time < 10 ? "0" + time : time + "");
    }

    /**
     * 刷新 时钟的 UI
     */
    private void refreshTimeUI() {

        String hourText = getTimeText(hour);
        String minuteText = getTimeText(minute);
        String secondText = getTimeText(second);

        timerString = hourText + ":" + minuteText + ":" + secondText;

        mTvHour.setText(hourText);
        mTvMinute.setText(minuteText);
        mTvSecond.setText(secondText);
    }

    public void stopTimer() {
        if (mTimerTask != null) {
            mTimerTask.stopTimer();
            mTimerTask = null;
        }
    }

    /**
     * 未开始
     */
    public void unTimer() {
        this.unTimer = true;
        refreshTimeUI();

        if (mTimerTask != null) {
            mTimerTask.stopTimer();
        }
    }

    /**
     * 获取剩余的时间
     */
    public long getLeaveTime() {
        return leaveTime;
    }

    /**
     * 设置剩余的时间
     */
    public void setLeaveTime(long leaveTime) {
        this.leaveTime = leaveTime;
        this.unTimer = false;

        calculateTime(leaveTime);

        if (mTimerTask == null) {
            mTimerTask = new TimerTask();
        }
        mTimerTask.startTimer();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mTimerTask != null) {
            mTimerTask.stopTimer();
        }
    }

    public void setOnTimeChangeListener(OnTimeChangeListener listener) {
        mListener = listener;
    }

    public interface OnTimeChangeListener {

        void onLeaveTimeChanged(int viewId, long leaveTime);
    }

    private class TimerTask implements Runnable {

        @Override
        public void run() {
            calculateTime(--leaveTime);

            // 每隔1秒重新计算
            UIUtils.postDelayed(this, 1000);
        }

        /**
         * 停止倒计时
         */
        private void stopTimer() {
            UIUtils.removeCallbacks(this);
        }

        /**
         * 开始倒计时
         */
        private void startTimer() {
            stopTimer();

            UIUtils.postDelayed(this, 1000);
        }
    }
}
